/************************************************************************
This file is part of java core libraries for the simpleRTJ virtual machine.

This file is covered by the GNU GPL with the following exception:
  As a special exception, the copyright holders of this library give you permission
  to link this library with independent modules to produce an executable, regardless
  of the license terms of these independent modules, and to copy and distribute the
  resulting executable under terms of your choice, provided that you also meet, for
  each linked independent module, the terms and conditions of the license of that
  module. An independent module is a module which is not derived from or based on
  this library. If you modify this library, you may extend this exception to your
  version of the library, but you are not obligated to do so. If you do not wish
  to do so, delete this exception statement from your version.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Created/modified by:
    RTJ Computing
***********************************************************************/
package java.lang;


/**
 * Encapsulates <strong>short</strong> java primitive type.
 *
 * For detailed description of this class see the Java language documentation.
 */
final public class Short extends Number
{
    final public static short MIN_VALUE = -0x8000;
    final public static short MAX_VALUE = 0x7FFF;
    private short val;

    public Short(String s) throws NumberFormatException
    {
        this(parseShort(s));
    }

    public Short(short value)
    {
        val = value;
    }

    public static Short decode(String nm) throws NumberFormatException
    {
        short val;

        if (nm.startsWith("0x"))
        {
            val = parseShort(nm.substring(2), 16);
        }
        else if (nm.startsWith("#"))
        {
            val = parseShort(nm.substring(1), 16);
        }
        else if (nm.startsWith("0"))
        {
            val = parseShort(nm.substring(1), 8);
        }
        else
        {
            val = parseShort(nm.substring(1), 10);
        }

        return new Short(val);
    }

    public boolean equals(Object obj)
    {
        try
        {
            if (((Short)obj).val == val)
            {
                return true;
            }
        }
        catch (ClassCastException e) {}

        return false;
    }

    public float floatValue()
    {
        return (float)val;
    }

    public int hashCode()
    {
        return val;
    }

    public int intValue()
    {
        return (int)val;
    }

    public static short parseShort(String s) throws NumberFormatException
    {
        return (short)parseShort(s, 10);
    }

    public static short parseShort(String s, int radix) throws NumberFormatException
    {
        return (short)Integer.parseInt(s, radix);
    }

    public String toString()
    {
        return toString(val);
    }

    public static String toString(short s)
    {
        return Integer.toString((int)s);
    }

    public static Short valueOf(String s) throws NumberFormatException
    {
        return new Short(parseShort(s));
    }

    public static Short valueOf(String s, int radix) throws NumberFormatException
    {
        return new Short(parseShort(s, radix));
    }
}
